package top.deepdesigner.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Start up <br/>
 * Created with IntelliJ IDEA. <br/>
 * Project Name: shop_manage <br/>
 * Author: duanruiqing <br/>
 * DateTime: 2022/9/22 星期四 10:29:26 <br/>
 */
public class Byte2StreamUtil {

  /**
   * byte转输入流
   *
   * @param bytes 字节
   * @return {@link InputStream}
   */
  public static InputStream byte2InputStream(byte[] bytes) {
    return new ByteArrayInputStream(bytes);
  }


  /**
   * 输入stream转byte
   *
   * @param inputStream 输入流
   * @return byte[] 数组
   * @throws IOException ioexception
   */
  public static byte[] inputStream2byte(InputStream inputStream) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    byte[] buff = new byte[100];
    int rc = 0;
    while ((rc = inputStream.read(buff, 0, 100)) > 0) {
      byteArrayOutputStream.write(buff, 0, rc);
    }
    return byteArrayOutputStream.toByteArray();
  }

}
